﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class Scheduler : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {

    }

    protected void PatientScheduler_FormCreating(object sender, SchedulerFormCreatingEventArgs e)
    {
        e.Appointment.Resources.Add(PatientScheduler.Resources.GetResourceByType("DoctorAppointment"));
        e.Appointment.Resources.Add(PatientScheduler.Resources.GetResourceByType("AppointmentState"));
        e.Appointment.Resources.Add(PatientScheduler.Resources.GetResourceByType("Patient"));
    }

    protected void PatientScheduler_AppointmentInsert(object sender, AppointmentInsertEventArgs e)
    {
        if (e.Appointment.Start > e.Appointment.End)
        {
            e.Cancel = true;
            throw new Exception("start time must be before end time");
        }

        e.Cancel = !ValidateConcurrencyTime();
    }
    protected void PatientScheduler_AppointmentUpdate(object sender, AppointmentUpdateEventArgs e)
    {
        TimeSpan endTime = e.ModifiedAppointment.End.TimeOfDay; 
        e.ModifiedAppointment.End = e.Appointment.End;
        e.ModifiedAppointment.End = e.ModifiedAppointment.End.Add(endTime - e.ModifiedAppointment.End.TimeOfDay);

        TimeSpan startTime = e.ModifiedAppointment.Start.TimeOfDay;
        e.ModifiedAppointment.Start = e.Appointment.Start;        
        e.ModifiedAppointment.Start = e.ModifiedAppointment.Start.Add(startTime - e.ModifiedAppointment.Start.TimeOfDay);

        if (e.ModifiedAppointment.Start > e.ModifiedAppointment.End)
        {
            e.Cancel = true;
            //TODO: Exceptio Handeling
            throw new Exception("start time must be before end time");
        }

        e.Cancel = !ValidateConcurrencyTime();
    }

    private bool ValidateConcurrencyTime()
    {
        return true;
    }
}
